<template>
  <div>
    <elm-page-tb>
      <template #top>
        <elm-row>
          <el-input placeholder="销售批次" size="mini" prefix-icon='el-icon-search' style="margin: 0 10px" v-model="postData.name" clearable></el-input>
          <elm-select placeholder="货物名称" size="mini" v-model="postData.dict" style="margin: 0 10px" url="/business/jxc/goods/dict/GetData" clearable></elm-select>
          <el-date-picker placeholder="起始日期" type="date" value-format="yyyy-MM-dd" size="mini" style="margin: 0 10px" v-model="postData.startDate" clearable></el-date-picker>
          <el-date-picker placeholder="结束日期" type="date" value-format="yyyy-MM-dd" size="mini" style="margin: 0 10px" v-model="postData.endDate" clearable></el-date-picker>
          <right>
            <el-button-group>
              <el-button icon="el-icon-search" size="mini" @click="tablebinding()">查询</el-button>
              <elm-open-button url="goods_out_edit" title="新增"  width="80%" height="80%">新增</elm-open-button>
            </el-button-group>
          </right>
        </elm-row>
      </template>
      <elm-table url="/business/jxc/goods/out/GetData" :post-data="postData" :page-size="10" height="100%">
        <el-table-column prop="name" label="销售批次" align="center" header-align="center"></el-table-column>
        <el-table-column prop="dict_mc" label="货物名称" align="center" header-align="center"></el-table-column>
        <el-table-column prop="unit" label="单位" align="center" header-align="center"></el-table-column>
        <el-table-column prop="quantity" label="销售数量" align="right" header-align="center"></el-table-column>
        <el-table-column prop="price" label="销售价格" align="right" header-align="center"></el-table-column>
        <el-table-column prop="tex" label="税率" align="center" header-align="center">
          <template v-slot="scope">
            {{scope.row.tex?scope.row.tex+"%":""}}
          </template>
        </el-table-column>
        <el-table-column prop="out_time" label="出库时间" align="center" header-align="center" :formatter="formatDateTime" width="160"></el-table-column>
        <el-table-column prop="out_person_mc" label="出库员" align="center" header-align="center" width="100"></el-table-column>
        <el-table-column prop="remarks" label="备注" align="center" header-align="center" width="200px"></el-table-column>
        <el-table-column fixed="right" label="废弃" width="70px" align="center">
          <template v-slot="scope">
            <div v-if="scope.row.is_disuse">已废弃</div>
            <el-button title="作废" @click="disuse(scope.row)" type="text" v-if="!scope.row.is_disuse">作废</el-button>
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="80px" align="center">
          <template v-slot="scope">
            <elm-open-button url="goods_out_stage" title="查看详情"  width="80%" height="60%" :data="{ out_id: scope.row.id }" type="text">查看详情</elm-open-button>
          </template>
        </el-table-column>
      </elm-table>
    </elm-page-tb>
  </div>

</template>

<script>
export default {
  extends: elmVueBase,
  data() {
    return {
      postData: {
        name: null,
        startDate:new Date(new Date().setDate(1)).toISOString().split('T')[0],
        endDate: new Date().toISOString().split('T')[0],
      }
    }
  },
  created() {
  },
  methods: {
    disuse(Val){
      this.$confirm('此操作将不可撤回, 是否确认废弃此项?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        center: true
      }).then(async () => {
        Val.is_disuse=1;
        await elm.post("business/jxc/goods/out/Upsert",Val);
        let res = await elm.post("business/jxc/goods/dict/GetData",{id:Val.dict});
        if(res.pagedata.length>0){
          let it=res.pagedata[0];
          it.quantity+=Number(Val.quantity);
          await elm.post("business/jxc/goods/dict/Upsert",it);
        }
        let re = await elm.post("business/jxc/goods/out/stage/GetData",{out_id:Val.id});
        for(let i =0;i<re.length;i++){
          let item=re[i];
          let r= await elm.post("business/jxc/goods/GetData",{id:item.goods_id});
          console.log(r);
          if(r.pagedata.length>0){
            let ite= r.pagedata[0];
            ite.quantity+=Number(item.quantity);
            await elm.post("business/jxc/goods/Upsert",ite);
          }
          else{
            item.id=item.goods_id;
            await elm.post("business/jxc/goods/Upsert",item);
          }
        }
        this.$message({
          type: 'success',
          message: '废弃成功!'
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消废弃'
        });
      });
    }
  }
}
</script>
